package com.example.agriculture.util;

import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object reglister = session.getAttribute("admininfor");
        Object producer = session.getAttribute("producer");
        Object Userinfo = session.getAttribute("Userinfo");

        if (request.getRequestURL().toString().contains("User")) {
            if (Userinfo != null) {
                return true;
            }else {
                request.setAttribute("msg", "请先登录!");
                request.getRequestDispatcher("/User/loginUser").forward(request, response);
                return false;
            }
        }

        if (reglister != null || producer != null) {
            return true;
        } else {
            request.setAttribute("msg", "请先登录!");
            request.getRequestDispatcher("/admin").forward(request, response);
            return false;
        }
    }
}
